package com.gitee.chili.leisure.game;

import com.gitee.chili.leisure.ResponseCode;
import com.gitee.chili.leisure.domain.Setting;
import com.gitee.chili.leisure.enums.CurrencyToken;
import com.gitee.chili.leisure.enums.RoomType;
import com.gitee.chili.leisure.exception.BusinessException;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.experimental.Tolerate;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * NotifyRoomInfoDTO
 *
 * @author week solider
 * @since 2020.09.04
 */
@Data
@Builder
@Accessors(chain = true)
public class RoomDetail {

    private Long roomNum;

    private Long gameType;

    private CurrencyToken pay;

    private int innings;

    private Date createTime;

    private Setting rules;

    private Long homeowner;

    private RoomType roomType;

    private int baseScore;

    private int cappedPoints;

    private Long lowerScore;

    private Long upperScore;

    private int peopleNum;

    private int bankerSeat;

    private int restCardsNum;

    private int drawCardPosition;

    private int discardSeat;

    private List<Integer> cards;

    private List<? extends Player> players;

    /**
     * 根据玩家ID获取玩家信息
     *
     * @param memberId 玩家id
     */
    public Player obtain(Long memberId) {

        Optional<? extends Player> optional = players.stream().filter(p -> p.userId.equals(memberId)).findFirst();

        if (optional.isPresent()) {
            return optional.get();
        }

        throw new BusinessException(ResponseCode.CACHE_MISS);
    }

    /**
     * 玩家摸牌
     *
     * @param player 玩家信息
     */
    public void draw(Player player) {
        AtomicInteger next = new AtomicInteger(drawCardPosition);
        player.draw(cards.get(next.get()));
    }

    /**
     * 定庄 (同时指定出牌的位置)
     */
    public void defineBanker(Player player) {
        int position = player.getPosition();
        bankerSeat = position;
        discardSeat = position;
    }

    @Tolerate
    public RoomDetail() {
    }
}
